<?php

/*
Plugin Name: BlueLight-Reloaded
Plugin URI: http://lloc.de/noch-mehr-zu-wordpress-themes-optimieren.html
Description: What's that? It's blue light. What does it do? It turns blue.
Version: 0.3
Author: Dennis Ploetner
Author URI: http://lloc.de/
License: GPL2
*/

if (!defined ('MY_DESCRIPTION_STR'))
    define ('MY_DESCRIPTION_STR', 'Deine individuelle Blogbeschreibung');
if (!defined ('MY_KEYWORDS_STR'))
    define ('MY_KEYWORDS_STR', 'Liste, von, Keywords, durch, Komma, getrennt');

function my_page_str ($prefix = ', ', $postfix = '') {
    $paged = (get_query_var ('paged') ? (int) get_query_var ('paged') : 1);
    return ($paged > 1 ? $prefix . __ ('Page') . ' ' . $paged . $postfix : '');
}

function my_title ($str) {
    $title = trim ($str);
    $postfix = ' | ' . get_bloginfo ('name');
    if (is_single() || is_page()) { 
	return $title . $postfix; 
    } elseif (is_home () || is_front_page ()) {
	$title = get_bloginfo ('description');
    } elseif (is_category ()) {
	$title = __ ('Category') . ' ' . $title;
    } elseif (is_archive ()) {
	$title = __ ('Archives') . ' ' . $title;
    }
    return $title . my_page_str () . $postfix;
}
add_filter ('wp_title', 'my_title');

function my_description () {
    if (
	is_home () ||
	is_front_page () ||
	is_single () ||
	is_page () ||
	is_category ()
    ) {
	$str = '';
	if (is_home() || is_front_page ()) {
	    $str = MY_DESCRIPTION_STR;
	} elseif (is_single() || is_page()) {
	    global $wp_query;
	    $str = $wp_query->post->post_content;
	} elseif (is_category()) {
	    $str = category_description ();
	}
	$str = my_page_str ('', ': ') . $str;
	$str = strip_tags ($str);
	if (strlen ($str) > 160) {
	    $str = mb_substr ($str, 0, 160);
	    $str = mb_split (' ', $str);
	    array_pop ($str);
	    $str = implode (' ', $str) . ' …';
	}

?>
<meta name="description" content="<?php echo htmlspecialchars ($str); ?>"/>
<?php

    }
}
add_action ('wp_head', 'my_description');

function my_keywords () {
    $str = '';
    if (is_home () || is_front_page ()) {
	$str = MY_KEYWORDS_STR;
    } elseif (is_single ()) {
	$keywords = array ();
	$arr = get_the_tags ();
        if ($arr) {
            foreach ($arr as $elem) {
                $keywords[] = strtolower ($elem->name);
            }
        }
        $arr = get_the_category ();
        if ($arr) {
            foreach ($arr as $elem) {
                $keywords[] = strtolower ($elem->name);
            }
        }
        $keywords = array_unique ($keywords);
	if (count ($keywords) > 10) $keywords = array_slice ($keywords, 0, 10);
	$str = implode (', ', $keywords);
    }
    if (!empty ($str)) {

?>
<meta name="keywords" content="<?php echo htmlspecialchars ($str); ?>"/>
<?php

    }
}
add_action ('wp_head', 'my_keywords');

function my_robots () {
    if (is_home () || is_front_page () || is_single () || is_page ()) {

?>
<meta name="robots" content="index, follow, noodp"/>
<?php

    } else {

?>
<meta name="robots" content="noindex, follow"/>
<?php

    }
}
add_action ('wp_head', 'my_robots');

remove_action ("wp_head", "rsd_link");
remove_action ("wp_head", "feed_links_extra", 3);
remove_action ("wp_head", "feed_links", 2);
remove_action ("wp_head", "wlwmanifest_link");
remove_action ("wp_head", "wp_generator");
remove_action ("wp_head", "index_rel_link");
remove_action ("wp_head", "parent_post_rel_link", 10, 0);
remove_action ("wp_head", "start_post_rel_link", 10, 0);
if (!remove_action ('wp_head', 'adjacent_posts_rel_link', 10, 0))
    remove_action ('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

?>
